package com.lottery.game.runner.v2;

import com.lottery.game.core.modules.service.app.OperationalSetService;
import com.lottery.game.core.modules.service.redis.RedisService;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

/**
 * 替换com.runshang.apps.cloudgame.runner.VirtualNumberSetRunner
 * @author Jake
 *
 */
@Slf4j
@Component
public class DummyCheckRunner {

	private final long INTERVAL = 600 * 1000L;

	@Autowired
	private OperationalSetService operationalSetService;

	@Autowired
	private RedisService redisFeignClient;

	@Scheduled(fixedRate = INTERVAL)
	public void checkDummy() {
		log.info("开始更新虚拟人数。。。");

		try {
			List<Map<String, Object>> list = operationalSetService.queryVirtualNumberSet();
			if(!CollectionUtils.isEmpty(list)) {
				list.forEach(item -> {
					String roomNumber = (String) item.get("roomNumber");// 获取房间号
					int max = Integer.parseInt((String) item.get("virtualNumberMax"));
					int min = Integer.parseInt((String) item.get("virtualNumber"));
					Random random = new Random();
					int randoms = random.nextInt(max - min + 1) + min;
					String randomInfo = String.valueOf(randoms);
					redisFeignClient.set(roomNumber, randomInfo);
				});
			}

		} catch (Exception e) {
			log.error("VirtualNumberSetRunner  error : " + e);
		}
	}

}
